package com.googlecode.cswish.struts.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.StrutsStatics;

import com.googlecode.cswish.Config;
import com.googlecode.cswish.struts.spring.ObjectFactory;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * Provide the common function operations
 * 
 * <pre>
 * <interceptor-ref name="function">
 * 		<param name="extFunc">functionName:javaClassName,functionName:javaClassName</param>
 * </interceptor-ref>
 * </pre>
 * 
 * Mark: java class can be instanced by spring factory.
 * 
 * Merge the function to PageElementInterceptor
 * 
 * @author Jerry.Feng Date: 2008-5-15
 */
@Deprecated 
public class FunctionInterceptor extends AbstractInterceptor {
	
	private static final Log logger = LogFactory.getLog(FunctionInterceptor.class);
	
	protected Map<String, Object> extFunctions = new HashMap<String, Object>();

	@Inject(StrutsConstants.STRUTS_ACTION_EXTENSION)
	private String actionExtension;
	
	@Resource
	private ObjectFactory objectFactory;
	
	@Resource
	private Config config;
	
	/**
	 * format is:<br>
	 * 		register name:class name, register name:class name,
	 * @param extFunc
	 */
	public void setExtFunc(String extFunc) {
		String[] functions = StringUtils.split(extFunc, ',');
		
		for (String function : functions) {
			int index = function.indexOf(':');
			
			if (index > 0) {
				String registerName = function.substring(0, index);
				String className = function.substring(index + 1);
				Object instance = null;
				try {
					instance = objectFactory.buildBean(className, null, true);
				} catch (Exception ex) {
					logger.error("Can't create function instance:" + className, ex);
				}
				if (instance != null) {
					extFunctions.put(registerName, instance);
				}
			}
		}
	}
	
	public String intercept(ActionInvocation invocation) throws Exception {
		
//    	Functions fn = (Functions) invocation.getInvocationContext().getApplication()
//    		.get(FrameConstant.STACK_FUNCTION);
//    	context.put(FrameConstant.STACK_FUNCTION, fn);
//    	request.setAttribute(FrameConstant.STACK_FUNCTION, fn);
    	
    	if (extFunctions.size() > 0) {
    		final ActionContext invocationContext = invocation.getInvocationContext();
    		HttpServletRequest request = (HttpServletRequest) invocationContext.get(StrutsStatics.HTTP_REQUEST);
    		
    		// register the basic function
        	ValueStack stack = invocation.getStack();
        	Map<String, Object> context = stack.getContext();
        	
    		for (String key : extFunctions.keySet()) {
    			Object exFn = extFunctions.get(key);
    			context.put(key, exFn);
    			request.setAttribute(key, exFn);
    		}
    	}
    	
        return invocation.invoke();
    }
}